﻿// This file is part of Code4Public (http://code4public.codeplex.com)
// Copyright 2011 Sina Iravanian - <sina@sinairv.com>
//
// This source file(s) may be redistributed, altered and customized
// by any means PROVIDING the authors name and all copyright
// notices remain intact.
// THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED. USE IT AT YOUR OWN RISK. THE AUTHOR ACCEPTS NO
// LIABILITY FOR ANY DATA DAMAGE/LOSS THAT THIS PRODUCT MAY CAUSE.
// ------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YAXLib;

namespace Code4Public.Languages
{
    [YAXSerializableType(FieldsToSerialize=YAXSerializationFields.AllFields)]
    public class CustomWordGroupInfo
    {
        public CustomWordGroupInfo() : this("", false)
        {
            
        }

        public CustomWordGroupInfo(string colorGroup, bool isCaseSensitive)
        {
            ColorGroup = colorGroup;
            IsCaseSensitive = isCaseSensitive;
        }

        public CustomWordGroupInfo(string colorGroup, bool isCaseSensitive, IEnumerable<string> words)
            : this(colorGroup, isCaseSensitive)
        {
            m_setWords.UnionWith(words);
        }

        [YAXAttributeFor(".#colorgroup")]
        public string ColorGroup { get; private set; }
        [YAXAttributeFor(".#casesensitive")]
        public bool IsCaseSensitive { get; private set; }

        [YAXCollection(YAXCollectionSerializationTypes.Serially, SeparateBy=" ")]
        [YAXValueForClass]
        private readonly HashSet<string> m_setWords = new HashSet<string>();

        [YAXDontSerialize]
        public string[] Words
        {
            get
            {
                return m_setWords.ToArray();
            }
        }

        public void AddWord(string word)
        {
            m_setWords.Add(word);
        }

        public void AddWords(IEnumerable<string> words)
        {
            m_setWords.UnionWith(words);
        }

        public override string ToString()
        {
            var sb = new StringBuilder();
            sb.AppendLine(String.Format("Custom Word Group - ColorGroup: {0} - words:", ColorGroup));

            sb.Append("[\r\n\t");

            sb.Append(GetWordsString());

            sb.AppendLine("\r\n]");

            return sb.ToString();
        }

        public string GetWordsString()
        {
            var sb = new StringBuilder();
            foreach (string word in m_setWords)
            {
                sb.Append(word + " ");
            }
            return sb.ToString();
        }

        public bool ContainsWord(string word)
        {
            if (!IsCaseSensitive)
                word = word.ToLower();

            return m_setWords.Contains(word);
        }
    }
}
